package fishGenerator;

import gameEntities.Fish;
import gameEntities.GameEntityManager;

import java.util.Random;

import math.Vector2D;

/**
 * The Class FishGenerator determines which functions fish generators should have.
 */
public abstract class FishGenerator {
	
	/**
	 * Instantiates a new fish generator.
	 *
	 * @param position the position
	 * @param depthRange the depth range where these fishes could appear
	 * @param depthStart the minimal depth
	 * @param right if the fish generator generates these fishes at the right end of the screen, or at the left
	 * @param speed the speed of the instantiated fishes
	 * @param gem the gem
	 */
	public FishGenerator(Vector2D position, int depthRange, int depthStart, boolean right, float speed, GameEntityManager gem) {
		this.position = position;
		this.depthRange = depthRange;
		this.depthStart = depthStart;
		this.speed = speed;
		this.random = new Random();
		random.setSeed((long)position.x * (long)position.y * 2);
		this.timer = 0.0f;
		this.right = right;
		this.gem = gem;
	}
	
	/**
	 * Dates the fish generator up and produces new fishes if a partly randomized function
	 * which depends on some constant values is true.
	 *
	 * @param time the delta time
	 */
	public void Update(float time) {
		while (timer < .0f) { // create new fish

			Vector2D p = position.duplicate();
			p.y += Math.abs(random.nextInt()) % depthRange + depthStart;
			
			Fish fish = CreateFish(p, right, speed);
			
			gem.Add(fish);
			
			timer += (float)Math.abs(random.nextInt() % 1000); // restart timer
		}
		
		timer -= time;
	}
	
	/**
	 * Creates a fish.
	 *
	 * @param position the position
	 * @param right if the fish should swim to the left (true), or to the right (false)
	 * @param speed the fish speed
	 * @return the fish
	 */
	protected abstract Fish CreateFish(Vector2D position, boolean right, float speed);
	
	private float timer;
	
	private final Random random;
	
	private final Vector2D position;
	
	private final int depthStart;
	private final int depthRange;
	private final float speed;
	private final boolean right;
	
	private final GameEntityManager gem;
}
